var app = angular.module('e-app', ['ngRoute']).filter(
    'to_trusted', ['$sce', function ($sce) {  
        return function (text) {  
            return $sce.trustAsHtml(text);  
        }  
    }]  
);
app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
}]);

app.controller('ylController',function($scope,$http) {
$scope.moduleList={};
$scope.checkLogin = function(){
    console.log(456);
    baas_yl.call({
        'http': $http,
        'method': 'get',
        'uri': 'manager/manager/checkLogin',

        'success': function(d) {
            if(d.login!='logged'){
                location.href='../user/login.html';
            }
            $scope.manager=d;
            $scope.manageName=d.managerGroup.groupName;
            $scope.manageTypeCode= d.type_code;
            $scope.manageId =d.manager_id;
            $scope.manage_account =d.manager_account;
            $scope.moduleList=d.moduleList;
            var manager={
                manager_id:d.manager_id,
                manager_group_id:d.managerGroup._id,
                manager_account:d.manager_account,
                type_code:d.type_code,
                company_filter_type:d.company_filter_type
            };
            var type_code = d.type_code;

            if(type_code=="company"||type_code=="companyService"||type_code=="pensionService"){
                if(typeof d.company_code != 'undefined' && d.company_code.length > 0 ){
                    manager.company_code=d.company_code[0];
                    var company=d.company[d.company_code[0]];
                    if(company==null || company==undefined){
                        alert("该企业账户，没有选择归属企业，请联系管理员");
                        return;
                    }
                    manager.company_name=company.name;
                    manager.serviceType3_code=company.serviceType3_code;
                    manager.serviceType3_title=company.serviceType3_title;
                    console.log(manager.serviceType3_code);
                }
            }
            if(type_code=="pensionService"){//是否为服务站
                manager.isPension=true;
            }
            if(type_code=="callCenter"){
                manager.isCallCenter=true;
            }
            var flag=manager.company_code+manager.manager_account+window.location.href;
            window.who=CRC32(flag);
            sessionStorage.setItem("who",window.who);
            localStorage.setItem(sessionStorage.getItem("who")+"manager",JSON.stringify(manager));

        }
    });
};
    $scope.checkLogin();
    $scope.loginout = function(){
        baas_yl.call({
            'http': $http,
            'method': 'post',
            'uri': 'manager/manager/logout',
            'success': function(d) {
            	
		          if(d.login!='logged'){
		          
		             location.href='user/login.html';
		        }
            }
        });
    };
     $scope.changePassword=function(manager){
     	
		if(manager.password==""){
            alert("请输入原始密码");
            return;
        }
         if(manager.new_password==""){
             alert("请输入新密码");
             return;
         }
         if(manager.qnew_password==""){
             alert("请输入确认密码");
             return;
         }
         if(manager.qnew_password!=manager.new_password){
             alert("新密码与确认密码不相同");
             return;
         }
		    	
         manager.account=$scope.manage_account;
         var data = {
             account      : manager.account,
             password     : manager.password,
             new_password : manager.new_password
         };
         baas_yl.call({
             'http': $http,
             'method': 'post',
             'uri': 'manager/manager/changePassword',
             'data' : data,
             'success': function(d) {
                 if(d.status == '1'){
                     alert("密码已修改，请重新登录!");
                     $scope.loginout();
                 }else{
                     if( d.description ){
                        alert("原始密码输入错误");
                     }else{
                        alert( '网络原因，请求失败，请稍后重试' );
                     }

                 }
             }
         });
        
		    	
     };
     $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        dropdown();
     })
});

app.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished');
                });
            }
        }
    };
});
	
	
	
function Baas(){}

Baas.url = {
    base       : 'http://bus.site4test.com/',
    init       : 'console/app/init',
    app_id     : '',
    app_secret : ''
};
Baas.prototype.getApiUrl = function( name ){
    var path = name.replace(/\./g,'/');
    var url = Baas.url.base + path;
    var keyUrl = "?app_id="+Baas.url.app_id + "&app_secret="+Baas.url.app_secret;
    url += keyUrl;
    return url;
};
Baas.prototype.request = function(url, data, requestType, fn){

    $.ajax({
        url : url,
        type: requestType,
        data : data,
        dataType : "json",
        success:function(json){
            if( !json.status ){
                console.log(json.description);
            }
            if( typeof fn == 'function'){
                fn(json);
            }
        }
    });
};

Baas.prototype.requestJsonp = function(url, data, callback){
    $.ajax({
        url : url,
        type:'get',
        data : data,
        dataType : "jsonp",
        jsonp: 'callback',
        jsonpCallback:callback,
        success:function(d){
           console.log(d);
        }
    });
};

Baas.init = function( app_id, app_secret ){

    Baas.url.app_id     = app_id;
    Baas.url.app_secret = app_secret;
    //var data = {
    //    app_id:app_id,
    //    app_secret:app_secret
    //};
    //var bs = new Baas();
    //var url = Baas.url.base + Baas.url.init;
    //bs.request(url, data, function( json ){
    //    if( !json.status ){
    //        console.log(json.description);
    //    }
    //});
};


Baas.prototype.api = function(  ){

    if(typeof arguments[0] == 'undefined'){
        console.log('请填写api名称');
        return;
    }
    var url = this.getApiUrl( arguments[0] );
    var data = ( typeof arguments[1] != 'undefined' ) ? arguments[1] : { };
    var requestType = ( typeof arguments[2] != 'undefined' ) ? arguments[2] : 'get';
    var callback = ( typeof arguments[3] != 'undefined' ) ? arguments[3] : 'callbackz';
    if(requestType == 'jsonp'){

        this.requestJsonp( url, data, callback );
    }else{
        this.request( url, data, requestType, callback );
    }

};

function CRC32(str){
    str=encodeURIComponent(str);
    var Crc32Table=new Array(256);
    var i,j;
    var Crc;
    for(i=0; i<256; i++)
    {
        Crc=i;
        for(j=0; j<8; j++)
        {
            if(Crc & 1)
                Crc=((Crc >> 1)& 0x7FFFFFFF) ^ 0xEDB88320;
            else
                Crc=((Crc >> 1)& 0x7FFFFFFF);
        }
        Crc32Table[i]=Crc;
    }
    if (typeof str != "string") str = "" + str;
    Crc=0xFFFFFFFF;
    for(i=0; i<str.length; i++)
        Crc=((Crc >> 8)&0x00FFFFFF) ^ Crc32Table[(Crc & 0xFF)^ str.charCodeAt(i)];
    Crc ^=0xFFFFFFFF;
    return (Crc >> 3).toString(16);
}
